<?
if ( ! defined('APP-START')) exit('No direct script access allowed');

/**
 * @FILE		contact_form.php
 * @DESC		a controller that submits a form
 * @PACKAGE		PASTEBOARD
 * @VERSION		1.0.0
 * @AUTHOR		James McCarthy
 * @EMAIL		james.mccarthy@gmail.com
 * @DATE		February 17, 2011	
 
	Copyright (C) 2011  James McCarthy, http://sjamesmccarthy.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 */
 
class contact_form extends app
{	
	public function contact_form()
	{	
		/* leave blank if you have no preload actions */
		/* you don't need to extend unless you need methods from app class */
		
		if(!file_exists('libraries/third-party/recaptchalib.php'))
		{
			$this->log_msg('contact_form(load_recaptcha) ' . 'libraries/third-party/recaptchalib.php', 'FATAL');
		} else { 
			include_once('libraries/third-party/recaptchalib.php');
			
			if($this->use_recaptcha != FALSE) $this->found_recaptcha = TRUE; 
		}
	}
		
	public function submit()
	{
		$message = NULL;
		$subject =NULL;
		$this->sender = $_POST['name'];
		
		if($this->found_recaptcha == TRUE && $this->use_recaptcha == TRUE)
		{			
			$privatekey = "INSERT_KEY_HERE";
			$resp = recaptcha_check_answer ($privatekey,
		                                $_SERVER["REMOTE_ADDR"],
		                                $_POST["recaptcha_challenge_field"],
		                                $_POST["recaptcha_response_field"]);
				
			if (!$resp->is_valid) {
		  		$_SESSION['form']['captcha_challenge'] = 'Failed';

		  		/* preserve the data submitted and auto-fill on reload */
		  		$_SESSION['form']['name'] = $_POST['name'];
		  		$_SESSION['form']['email'] = $_POST['email'];
		  		$_SESSION['form']['msg'] = $_POST['message'];
		  		
		  		/* redirect back to contact form */
		  		header('location:/page/contact');
		  		exit;
			} 
		
		} 
		
		/* we need to verify for a valid email address here */
		
		/* Set some basic headers */
		$headers  = 'From: ' . $_POST['name'] . '<' . $_POST['email'] . ">\r\n";
		$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
	
		$to = $this->CONTACT_EMAIL . "\r\n";
		
		if(!$_POST['subject']) 
		{
			$subject = "NO_SUBJECT_INPUT_FROM_FORM";
		} else {
			$subject = $_POST['subject'] . "\r\n";
		}
		
		if(!$_POST['message'])
		{
			foreach($_POST as $key => $value)
			{
				/* change to an exceptions array */
				if($key != 'x' && $key != 'y') { $message .= $key . ": " . $value . "\n"; }
			}
		} else {
			$message  = stripslashes($_POST['message']) . "\r\n";
		}
		
		mail($to, $subject, $message, $headers);
		
		/* unset $_SESSSION vars */
		unset($_SESSION['form']);
		
		$this->log_msg('contact_form(success-mailed to ' . $this->sender . ')', 'OK');
		$this->view_key = 'contact_thankyou';
		$this->tpl_key = 'page';
	}
}